home *** CD-ROM | disk | FTP | other *** search
Korn shell script | 1997-08-26 | 6.8 KB | 259 lines |
- #!/bin/ksh
- # fman: new man program
- # @(#) fman.ksh 1.5 94/04/16
- # 91/07/03 john h. dubois iii (john@armory.com)
- # 91/07/11 made it unpack man pages if neccessary
- # 91/07/16 fixed test for whether man file pattern was expanded
- # 92/01/21 made it read /etc/default/man to get section order,
- # and only display the first section found.
- # 92/02/06 changed name to fman
- # 92/02/07 fixed bug in notfound
- # 92/02/13 incorporated changes from DOS version
- # 92/03/11 changed to use MANPATH from environment if set,
- # and search all directories given in MANPATH
- # 92/03/15 exec pager or man w/o forking
- # 92/05/31 try using index if one exists
- # 92/10/01 Added "See also <other sections>"
- # 92/10/18 If PAGER is less, search for name of man page to make it easier
- # to find information in man pages for multiple items
- # 92/11/11 Make it work for compressed files not listed in index;
- # deal with man pages listed in index that don't exist.
- # 93/03/30 Fixed bug in MANPATH processing
- # 93/06/17 Include paths in "See also:" message if they would be needed
- # to get to a man page. Allow MANPATH spec on command line.
- # 93/07/09 Added -h and -e options.
- # 94/04/16 Added x option.
-
- alias istrue="test 0 -ne"
- alias isfalse="test 0 -eq"
-
- # Finds all sections that man page $1 is in and puts them in the the
- # global array Sections[].
- # The filename of each page is put in FileNames[] with the same index.
- # Global vars used:
- # patharr[] MANPATH directories.
-
- function FindSectionsInIndex {
- typeset index indexes section mpath page=$1
- typeset -i i=0 NIndex=0
-
- for mpath in "${patharr[@]}"; do
- if [ -r $mpath/index ]; then
- indexes="$indexes $mpath/index"
- let NIndex+=1
- fi
- done
- [ -z "$indexes" ] && return
- # Make egrep give filename
- [ NIndex -lt 2 ] && indexes="$indexes /dev/null"
- # set positional parameters to
- # indexfile:searchname pagename section ...
- # e.g.
- # /usr/man/index:FP_OFF Routines DOS
- set -- `egrep "^$page[ ]" $indexes`
- while [ $# -gt 2 ]; do
- FileNames[i]=${1%%index*}cat.$3/$2.$3
- Sections[i]=$3
- shift 3
- let i+=1
- done
- }
-
- # Finds all sections that man page $1 is in by searching each man directory
- # in the order given in patharr[],
- # and puts them in the the global array Sections[].
- # The filename of each page is put in FileNames[] with the same index.
- # Global vars used:
- # patharr[] MANPATH directories.
- function FindSectionsInDirs {
- typeset page=$1 mpath AllPaths Path
- typeset -i i
-
- for mpath in "${patharr[@]}"; do
- AllPaths="$AllPaths $mpath/cat.*/$page.*"
- done
-
- i=0
- for Path in $AllPaths; do
- if [[ "$Path" != *\* ]]; then
- # Remove compressed-file suffix to make FileNames be the same
- # as it is when built by FindSectionsInIndex()
- FileNames[i]=${Path%.[zZ]}
- Path=${Path%/*}
- Sections[i]=${Path##*/*.}
- let i+=1
- fi
- done
- }
-
- # FindSection: display man page.
- # Uses ordarr[] (built from $ORDER) to display the version of the man
- # page that occurs first in $ORDER.
- # Sections[] gives the sections that a man page was found in.
- # If the global variable "exist" is set to 1, nothing is displayed;
- # the function instead returns zero if a page is found, nonzero if not.
- # The filename of each page is in FileNames[] with the same index.
- # Global vars used:
- # Sections[], FileNames[], ordarr[]
- function FindSection {
-
- typeset -i NumPages i foundsec
- typeset section OtherSec filename NPAGER=$PAGER POpt page=$1 Pat
- typeset PageFile
-
- NumPages=${#Sections[*]} # Number of versions of man page found.
- isfalse NumPages && return 1
- [[ "$PAGER" = *less ]] && POpt="-p$page"
-
- # For each section in ORDER, determine if any man page was found in
- # that section
- for section in "${ordarr[@]}"; do
- i=0
- foundsec=0
- while [ i -lt NumPages ]; do
- if [ "${Sections[i]}" = $section ]; then
- # Found a man page from this section of ORDER
- filename=${FileNames[i]}
- if [ -z "$PageFile" ]; then
- PageFile=$filename
- else
- if istrue foundsec; then
- OtherSec="$OtherSec$page(${filename%/*/*} $section) "
- else
- OtherSec="$OtherSec$page($section) "
- fi
- fi
- foundsec=1
- fi
- let i+=1
- done
- done
- # No pages with the specified section found.
- [ -z "$PageFile" ] && return 1
- # Return if all we want to know is whether the man page exists.
- [ "$exist" = 1 ] && return 0
- if [ -z "$OtherSec" ]; then
- NPAGER="exec $PAGER"
- fi
- if [ -r $PageFile ]; then
- $NPAGER $POpt $PageFile
- elif [ -r $PageFile.z ]; then
- pcat $PageFile.z | $NPAGER $POpt
- elif [ -r $PageFile.Z ]; then
- zcat $PageFile.Z | $NPAGER $POpt
- else
- print -u2 "$PageFile: cannot open."
- OtherSec=
- unset Sections[i]
- let i+=1
- continue
- fi
- echo "See also $OtherSec"
- exit 0
- }
-
- # main program
-
- typeset -i exist=0 debug=0
-
- name=${0##*/}
- Usage="Usage: $name [-eh] [[manpath] section] command-name"
-
- while getopts :hex opt; do
- case $opt in
- h)
- echo \
- "$name: print man pages.
- $name locates and prints the specified manual pages from the online UNIX
- documentation.
- $Usage
- Options:
- -e: Determine whether the specified man page exists. Nothing is printed;
- $0 exits with a zero status if the page exists and a nonzero status if
- it does not.
- -h: Print this help."
- exit 0
- ;;
- e)
- exist=1
- ;;
- x)
- debug=1
- ;;
- # f)
- # files="$files $OPTARG" # add arg to list of address files
- # ;;
- +?)
- print -u2 "$name: options should not be preceded by a '+'."
- exit 1
- ;;
- ?)
- print -u2 "$name: $OPTARG: bad option. Use -h for help."
- exit 1
- ;;
- esac
- done
-
- # remove args that were options
- let OPTIND=OPTIND-1
- shift $OPTIND
-
- if [ $# -lt 1 ]; then
- print -u2 "$Usage\nUse -h for help."
- exit
- fi
-
- P=$PAGER
- O=$ORDER
- T=$TERM
- M=$MANPATH
- . /etc/default/man
- [ -n "$P" ] && PAGER=$P
- [ -n "$O" ] && ORDER=$O
- [ -n "$T" ] && TERM=$T
- [ -n "$M" ] && MANPATH=$M
-
- case $# in
- 0) print "No man page specified."
- exit 1;;
- 1) page=$1;;
- 2) typeset -u ORDER=$1
- page=$2;;
- 3) MANPATH=$1
- [ -n "$2" ] && typeset -u ORDER=$2
- page=$3;;
- *) print "Too many arguments."
- exit 1;;
- esac
-
- args=$*
- [ ! -t 0 ] && PAGER=cat
- OIFS=$IFS
- IFS=:
- set -A ordarr $ORDER
- set -A patharr $MANPATH
- IFS=$OIFS
- # if less or more is being used, remove multiple blank lines
- export LESS="-s $LESS"
- export MORE="-s $MORE"
-
- # Try using index
- FindSectionsInIndex "$page"
- # Exit 0 if a page was found and we're just testing for existence.
- FindSection "$page" && exit 0
-
- # Try searching directories
- unset Sections[*]
- FindSectionsInDirs "$page"
- FindSection "$page" && exit 0
-
- istrue exist && exit 1
-
- # Try using man
- # If using more or less, make man run faster by letting more or less compress
- # multiple blank lines instead of rmb
- [[ "$PAGER" = *@(more|less) ]] && manopt=-b
- set -A cmd -- /usr/bin/man $manopt -p$PAGER $args
- istrue debug && print -u2 "$name: running ${cmd[*]}"
- exec "${cmd[@]}"
-